package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.constant.OrderType;
import pers.cj.pdos.entity.InGoodsOrder;
import pers.cj.pdos.entity.InGoodsOrderGoods;
import pers.cj.pdos.entity.SaleOrder;
import pers.cj.pdos.entity.SaleOrderGoods;
import pers.cj.pdos.mapper.InGoodsOrderGoodsMapper;
import pers.cj.pdos.mapper.InGoodsOrderMapper;
import pers.cj.pdos.mapper.OrderRuleMapper;
import pers.cj.pdos.mapper.SaleOrderGoodsMapper;
import pers.cj.pdos.service.IInGoodsOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.InGoodsOrderVo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class InGoodsOrderServiceImpl extends ServiceImpl<InGoodsOrderMapper, InGoodsOrder> implements IInGoodsOrderService {
    @Autowired
    InGoodsOrderMapper inGoodsOrderMapper;
    @Autowired
    OrderRuleMapper orderRuleMapper;
    @Autowired
    InGoodsOrderGoodsMapper inGoodsOrderGoodsMapper;
    @Override
    public void batchVerify(String verifyUserId,List<InGoodsOrder> list) {
        inGoodsOrderMapper.batchVerify(verifyUserId,list);
    }

    @Override
    public void batchBackVerify(List<InGoodsOrder> list) {
        inGoodsOrderMapper.batchBackVerify(list);
    }

    @Override
    public boolean checkNo(String no, String companyId) {

        return inGoodsOrderMapper.selectCount(new QueryWrapper<InGoodsOrder>().eq("company_id",companyId).eq("no",no)) > 0;
    }

    @Override
    public void insert(InGoodsOrder inGoodsOrder, List<InGoodsOrderGoods> inGoodsOrderGoodsList) {
        inGoodsOrderMapper.insert(inGoodsOrder);
        inGoodsOrderGoodsMapper.insertBatch(inGoodsOrderGoodsList);
    }

    @Override
    public void update(InGoodsOrder inGoodsOrder, List<InGoodsOrderGoods> inGoodsOrderGoodsList) {
        inGoodsOrderMapper.updateById(inGoodsOrder);
        inGoodsOrderGoodsMapper.delete(new QueryWrapper<InGoodsOrderGoods>().eq("in_goods_order_id",inGoodsOrder.getId()));
        inGoodsOrderGoodsMapper.insertBatch(inGoodsOrderGoodsList);
    }

    @Override
    public InGoodsOrderVo getVoById(String id) {
        return inGoodsOrderMapper.getVoById(id);
    }

    @Override
    public List<InGoodsOrderVo> listVo(String companyId,
                                       Integer page,
                                       Integer limit,
                                       Boolean close,
                                       String search,
                                       String no,
                                       String goodsName,
                                       String remark,
                                       String provider,
                                       Date makeTime1,
                                       Date makeTime2,
                                       String storeId,
                                       String userStoreId,
                                       String userId,
                                       Date date1,
                                       Date date2,
                                       String makeUserId,
                                       Integer status) {
        return inGoodsOrderMapper.listVo(companyId,
                Utils.genPage(page,limit),
                limit,
                close,
                search,
                no,
                goodsName,
                remark,
                provider,
                makeTime1,
                makeTime2,
                storeId,
                userStoreId,
                userId,
                date1,
                date2,
                makeUserId,
                status);
    }

    @Override
    public long count(String companyId,
                      Boolean close,
                      String search,
                      String no,
                      String goodsName,
                      String remark,
                      String provider,
                      Date makeTime1,
                      Date makeTime2,
                      String storeId,
                      String userStoreId,
                      String userId,
                      Date date1,
                      Date date2,
                      String makeUserId,
                      Integer status) {
        return inGoodsOrderMapper.count(companyId,
                close,
                search,
                no,
                goodsName,
                remark,
                provider,
                makeTime1,
                makeTime2,
                storeId,
                userStoreId,
                userId,
                date1,
                date2,
                makeUserId,
                status);
    }

    @Override
    public List<InGoodsOrderVo> listVoByIds(List<String> ids) {
        return inGoodsOrderMapper.listVoByIds(ids);
    }

    @Override
    public void close(InGoodsOrder inGoodsOrder) {
        InGoodsOrder temp = new InGoodsOrder();
        temp.setId(inGoodsOrder.getId());
        temp.setClose(true);
        inGoodsOrderMapper.updateById(temp);
    }

    @Override
    public boolean checkClose(InGoodsOrder inGoodsOrder) {
        return inGoodsOrderMapper.selectCount(new QueryWrapper<InGoodsOrder>().eq("id",inGoodsOrder.getId()).eq("close",true))  > 0;
    }
}
